package com.fxcalibre.db.beans;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.Hibernate;

@Entity
@Table(name = "MEDIA")
public class MediaItem extends HibernateBean
{

	@Id
	@GeneratedValue
	@Column(name = "ITEM_ID", nullable = false)
	long itemId;

	@Column(name = "IMDB_ID", nullable = true)
	String imdbId;

	@ManyToOne(fetch = FetchType.LAZY, cascade =
	{ CascadeType.ALL })
	@JoinColumn(name = "parent_id")
	private MediaItem parent;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
	private Set<MediaItem> children = new HashSet<MediaItem>();

	@Column(name = "NAME", nullable = false)
	String name;

	@Column(name = "ORIGINAL_NAME", nullable = true)
	String originalName;

	@Column(name = "YEAR")
	String year;

	@Column(name = "SEASON", nullable = true)
	Integer season;

	@Column(name = "EPISODE", nullable = true)
	Integer episode;

	@Column(name = "TYPE", nullable = true)
	String type;

	@Column(name = "FILE", nullable = true)
	String file;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.mediaItem", cascade = CascadeType.ALL, orphanRemoval = true)
	Set<Crew> crew = new HashSet<Crew>();

	@Override
	public void initialize()
	{
		Hibernate.initialize(getCrew());
		Hibernate.initialize(getParent());
		Hibernate.initialize(getChildren());
	}

	public String getImgName()
	{
		return "title_" + getItemId() + ".jpg";
	}

	public long getItemId()
	{
		return itemId;
	}

	public void setItemId(long itemId)
	{
		this.itemId = itemId;
	}

	public String getFile()
	{
		return file;
	}

	public void setFile(String file)
	{
		this.file = file;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getYear()
	{
		return year;
	}

	public void setYear(String year)
	{
		this.year = year;
	}

	public String getType()
	{
		return type;
	}

	public void setType(String type)
	{
		this.type = type;
	}

	public String getImdbId()
	{
		return imdbId;
	}

	public void setImdbId(String imdbId)
	{
		this.imdbId = imdbId;
	}

	public Integer getSeason()
	{
		return season;
	}

	public void setSeason(Integer season)
	{
		this.season = season;
	}

	public Integer getEpisode()
	{
		return episode;
	}

	public void setEpisode(Integer episode)
	{
		this.episode = episode;
	}

	public String getOriginalName()
	{
		return originalName;
	}

	public void setOriginalName(String originalName)
	{
		this.originalName = originalName;
	}

	public Set<Crew> getCrew()
	{
		return crew;
	}

	public void setCrew(Set<Crew> crew)
	{
		this.crew = crew;
	}

	public MediaItem getParent()
	{
		return parent;
	}

	public void setParent(MediaItem parent)
	{
		this.parent = parent;
	}

	public Set<MediaItem> getChildren()
	{
		return children;
	}

	public void setChildren(Set<MediaItem> children)
	{
		this.children = children;
	}

	@Override
	public String toString()
	{
		return "MediaItem [itemId=" + itemId + ", imdbId=" + imdbId + ", name=" + name + "]";
	}

}
